package com.ice.app.system.dict.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.iceframework.core.entity.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ice.app.system.dict.service.DictService;
import com.ice.app.system.dict.vo.DictVo;
import com.ice.app.system.entity.Dict;
import com.iceframework.core.base.BaseController;
import com.iceframework.core.entity.Result;

/**
 * 
 * 控制层： 数据字典表
 * 
 * @author Mr.Palo
 * @email 442803840@qq.com
 * 
 */
@RestController
@RequestMapping("/dict")
public class DictController extends BaseController {

	@Autowired
	private DictService dictService;

	/**
	 * 获取数据
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/get", method = RequestMethod.GET)
	public Result getDict(@RequestParam(value = "key") String key, HttpServletRequest request) {
		Dict dict = dictService.getDict(key);
		return Result.success().add("dict", dict);
	}

	/**
	 * 首页加载数据
	 * 
	 * @param pn
	 * @param dict
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/index/data", method = RequestMethod.POST)
	public Result dictIndexData(Dict dict, Page<Dict> page, HttpServletRequest request) {
		PageHelper.startPage(page.getPageNum(), page.getPageSize(), page.getOrderBy());
		List<Dict> dicts = dictService.getDicts(dict);
		PageInfo<Dict> data = new PageInfo<Dict>(dicts, page.getPageSize());
		return Result.success().add("data", data);
	}

	/**
	 * 保存实体
	 * 
	 * @param dict
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public Result saveDict(Dict dict) throws Exception {
		if (dictService.saveDict(dict)) {
			return Result.saveSuccess();
		} else {
			return Result.saveFail();
		}
	}

	/**
	 * 删除实体
	 * 
	 * @param keys
	 * @return
	 */
	@RequestMapping(value = "/del", method = RequestMethod.POST)
	public Result delDict(String keys) {
		if (dictService.deleteDicts(keys)) {
			return Result.delSuccess();
		} else {
			return Result.delFail();
		}
	}

	/**
	 * 前端字典下拉选
	 * 
	 * @param dictCode
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/select", method = RequestMethod.GET)
	public Result dictIndexData(@RequestParam("dictCode") String dictCode, HttpServletRequest request) {
		List<DictVo> dicts = dictService.getDictVoByDictCode(dictCode);
		return Result.success().add("data", dicts);
	}

}